package com.misolab.game.sbh.data.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.misolab.game.sbh.data.db.table.ContentTable;
import com.misolab.game.sbh.data.vo.LevelDatas;
import com.misolab.game.sbh.util.log.Logger;

public class ContentDBHelper implements DBConstant {
	final static String ClassName = ContentDBHelper.class.getSimpleName();

	static void logFunc(String functionName) {
		Logger.i(ClassName + "::" + functionName+"()");
	}

	private final Context mContext;
	
	


	public ContentDBHelper(Context ctx) {
		super();
		logFunc("ContentDBHelper");

		this.mContext = ctx;
		DBHelper dbHelper = new DBHelper(mContext);
		dbHelper.getmTableList().add(new ContentTable());
		dbHelper.open();
		dbHelper.close();
	}
	
	public String getContentWithLevel(int level){
		logFunc("getContentWithLevel");

		String retValue = "";
		try {
			String sql = String.format("select * from %s where LEVEL=%d order by COUNT", DB_TABLE_NAME,level);

			DBHelper dbHelper = new DBHelper(mContext);
			dbHelper.open();
			Cursor cursor = dbHelper.rawQuery(sql); 
			
			cursor.moveToFirst();
			int id = cursor.getInt(0);
			retValue = cursor.getString(1);
			int count = cursor.getInt(3);
			cursor.close();
			
			dbHelper.begin();
			sql = String.format("update %s set COUNT=%d where ID=%d", DB_TABLE_NAME, ++count, id);
			dbHelper.rawExecSQL(sql);
			dbHelper.commit();

			dbHelper.close();
			
		} catch (Exception e) {
			Logger.e( e.getMessage() );
			retValue = null;
		}
		return retValue;
	}
	
	public int setData(ArrayList<LevelDatas> items){
		logFunc("setData");

		int retValue = 0;
		try {
			DBHelper dbHelper = new DBHelper(mContext);
			dbHelper.open();
			
			dbHelper.begin();
			//	delete
			String sql = String.format("delete from %s", DB_TABLE_NAME);
			dbHelper.rawExecSQL(sql);
			
			//	insert
			for (LevelDatas levelDatas : items) {
				int level = levelDatas.getLevel();
				ArrayList<String> contentList = levelDatas.getContentList();
				for (String content : contentList) {
					sql = String.format("insert into %s (%s, %s) values ('%s', %d)", 
							DB_TABLE_NAME, DB_COLUMN_CONTENT_NAME, DB_COLUMN_LEVEL_NAME,
							content, level);
					dbHelper.rawExecSQL(sql);
				}
			}
			
			dbHelper.commit();
			dbHelper.close();			
		} catch (Exception e) {
			Logger.e( e.getMessage() );
			retValue = -1;
		}

		return retValue;
	}

	public int getCount() {
		logFunc("getCount");
		int retValue =0;
		
		try {
			String sql = String.format("select count(*) from %s", DB_TABLE_NAME);

			DBHelper dbHelper = new DBHelper(mContext);
			dbHelper.open();
			Cursor cursor = dbHelper.rawQuery(sql); 
			
			cursor.moveToFirst();
			retValue = cursor.getInt(0);
			cursor.close();

			dbHelper.close();			
		} catch (Exception e) {
			Logger.e( e.getMessage() );
			retValue = -1;
		}
		return retValue;
	}
}
